package com.servlet;

import com.entity.Users;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/updataAdmin")
public class UpdataAdminServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Users users = new Users();
        users.setUsername(req.getParameter("user"));
        users.setSex(req.getParameter("gender"));
        users.setPwdHash(req.getParameter("pwd"));
        String birthday = req.getParameter("birthdayu");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date= null;
        java.sql.Date sqlDate=null;
        try {
            date = format.parse(birthday);
            sqlDate = new java.sql.Date(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        users.setBirthday(sqlDate);
        String usertype = req.getParameter("type");
        int i = Integer.parseInt(usertype);
        users.setUsertype(i);
        req.setAttribute("userUpdate",users);
        req.getRequestDispatcher("/admin/add_user.jsp").forward(req,resp);
    }
}
